//
//  DrawerViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/4/6.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "DrawerViewController.h"
#import "DrawerTableViewCell.h"
#import "DrawerModel.h"
#import "BrandModel.h"
#import "FindDetailViewController.h"


@interface DrawerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *tableArr;

@end

@implementation DrawerViewController
-(void)dealloc
{
    [_tableV release];
    [_tableArr release];
    [_drawerId release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
//    [self dataHandle];
}

- (void)setDrawerId:(NSString *)drawerId
{
    if (_drawerId != drawerId) {
        [_drawerId release];
        _drawerId = [drawerId retain];
    }
    [self dataHandle];
    
}

#pragma mark - 抽屉的数据解析
- (void)dataHandle
{
    NSLog(@"====%@", self.drawerId);
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/modelLibraryHomePage?bid=%@&type=1", self.drawerId];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        NSMutableArray *oneArr = responseObject[@"manufacturers"];
        self.tableArr = [BrandModel transformWithArray:oneArr];
        
        [_tableV reloadData];
        
        
//        self.dataArr=[NSMutableArray array];
//        for (NSDictionary *dic in responseObject[@"sections"]) {
//            NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
//            [self.dataArr addObject:dic1];
//            NSMutableArray *arr = [NSMutableArray array];
//            [dic1 setObject:arr forKey:@"brands"];
//            [dic1 setObject:dic[@"index"] forKey:@"index"];
//            for (NSDictionary *temp in dic[@"brands"]) {
//                CarListModel *model = [[CarListModel alloc]initWithDic:temp];
//                [arr addObject:model];
//            }
//            
//        }
    } filed:^(NSError *error) {
        
    }];
    
}

#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 60 - 49) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableV];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.rowHeight = 70;
    [_tableV release];
    
    [self.tableV registerClass:[DrawerTableViewCell class] forCellReuseIdentifier:@"DrawerTableViewCell"];
}

#pragma mark - tableView的两个协议方法
#pragma mark - 设置cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BrandModel *brandModel = self.tableArr[section];
    
    return brandModel.serialArr.count;
    
//    return [[self.tableArr[section] serialArr] count];
}

#pragma mark - 设置cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrawerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DrawerTableViewCell"];
    
    BrandModel *BModel = self.tableArr[indexPath.section];
    DrawerModel *DModel = BModel.serialArr[indexPath.row];
    
    [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:DModel.photo]];
    cell.nameLab.text = DModel.name;
    cell.priceLab.text = DModel.priceRange;
    cell.typeLab.text = DModel.kind;
    
    return cell;
}

#pragma mark - 设置分区头的内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    BrandModel *BModel = self.tableArr[section];
    return BModel.name;
    
}


#pragma mark - 设置分区的数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tableArr.count;
}


#pragma mark - 设置点击的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailViewController *findDetail = [[FindDetailViewController alloc] init];
    
    // 将抽屉里面的id 传值给FindDetailViewControl里面
    BrandModel *brandM = self.tableArr[indexPath.section];
    findDetail.findDetailId = [brandM.serialArr[indexPath.row] mId];
    findDetail.priceRange = [brandM.serialArr[indexPath.row] priceRange];
    NSLog(@"%@", findDetail.findDetailId);
    [self.navigationController pushViewController:findDetail animated:YES];
    
    [findDetail release];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
